package cn.tedu.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import java.io.IOException;

/**
 * @author Administrator
 */
public class HDFSDemo {

    public static void main(String[] args) throws IOException {
        //创建Configuration配置类对象
        Configuration conf = new Configuration();
        //设置连接dfs的配置地址
        conf.set("fs.default.name", "hdfs://hadoop01:8020");
        FileSystem fs = FileSystem.get(conf);
        System.out.println(fs);
    }

    @Test
    public void mkdir() throws IOException {
        //构建conf对象
        Configuration conf = new Configuration();
        //设置属性,hdfs连接地址
        conf.set("fs.defaultFS", "hdfs://hadoop01:8020");
        //获取文件对象
        FileSystem fs = FileSystem.get(conf);
        //创建目录
        fs.mkdirs(new Path("/test/input"));
        //关闭fs
        fs.close();
    }

    @Test
    public void uploadFile() throws IOException {
        //构建conf对象
        Configuration conf = new Configuration();
        //设置属性,hdfs连接地址
        conf.set("fs.defaultFS", "hdfs://hadoop01:8020");
        //获取文件对象
        FileSystem fs = FileSystem.get(conf);
        fs.copyFromLocalFile(new Path("G:\\aaa.txt"), new Path("/test/input"));

        fs.close();
    }

    @Test
    public void downloadFile() throws IOException {
        Configuration conf = new Configuration();
        //设置属性,hdfs连接地址
        conf.set("fs.defaultFS", "hdfs://hadoop01:8020");
        //获取文件对象
        FileSystem fs = FileSystem.get(conf);
        fs.copyToLocalFile(new Path("/test/input/aaa.txt"), new Path("G:\\bbb.txt"));
        fs.close();
    }
}
